package cc.zstart.utils;

import cc.zstart.exception.LogicException;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidateUtil {
	private static final String MOBILE_REGX = "(13|14|15|17|18)\\d{9}";//mobile regx
	private static final String EMAIL_REGX = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";//email regx

	public static void handleMsg(String msg, Integer code) {
		throw new LogicException(msg, code);
	}

	/**
	 * 如果是blank 则异常处理
	 * @param str
	 * @param msg
	 * @param code
	 */
	public static void ifBlank(String str, String msg, Integer code) {
		if (StringUtils.isBlank(str)) {
			handleMsg(msg, code);
		}
	}
	public static void ifBlank(String str, RuntimeException e) {
		if (StringUtils.isBlank(str)) {
			throw e;
		}
	}
	public static void ifBlank(String str, String msg) {
		ifBlank(str, msg, null);
	}
	public static void ifBlank(String str, Integer code) {
		ifBlank(str, null, code);
	}
	public static void ifBlank(String str) {
		ifBlank(str, null, null);
	}

	/**
	 * 如果不是blank 则异常处理
	 * @param str
	 * @param msg
	 * @param code
	 */
	public static void ifNotBlank(String str, String msg, Integer code) {
		if (StringUtils.isNotBlank(str)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNotBlank(String str, RuntimeException e) {
		if (StringUtils.isNotBlank(str)) {
			throw e;
		}
	}
	public static void ifNotBlank(String str, String msg) {
		ifNotBlank(str, msg, null);
	}
	public static void ifNotBlank(String str, Integer code) {
		ifNotBlank(str, null, code);
	}
	public static void ifNotBlank(String str) {
		ifNotBlank(str, null, null);
	}

	
	/**
	 * 如果是empty 则异常处理
	 * @param str
	 * @param msg
	 * @param code
	 */
	public static void ifEmpty(String str, String msg, Integer code) {
		if (StringUtils.isEmpty(str)) {
			handleMsg(msg, code);
		}
	}
	public static void ifEmpty(String str, RuntimeException e) {
		if (StringUtils.isEmpty(str)) {
			throw e;
		}
	}
	public static void ifEmpty(String str, String msg) {
		ifEmpty(str, msg, null);
	}
	public static void ifEmpty(String str, Integer code) {
		ifEmpty(str, null, code);
	}
	public static void ifEmpty(String str) {
		ifEmpty(str, null, null);
	}

	/**
	 * 如果不是empty 则异常处理
	 * @param str
	 * @param msg
	 * @param code
	 */
	public static void ifNotEmpty(String str, String msg, Integer code) {
		if (StringUtils.isNotEmpty(str)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNotEmpty(String str, RuntimeException e) {
		if (StringUtils.isNotEmpty(str)) {
			throw e;
		}
	}
	public static void ifNotEmpty(String str, String msg) {
		ifNotEmpty(str, msg, null);
	}
	public static void ifNotEmpty(String str, Integer code) {
		ifNotEmpty(str, null, code);
	}
	public static void ifNotEmpty(String str) {
		ifNotEmpty(str, null, null);
	}
	
	/**
	 * 如果是null 则异常处理
	 * @param obj
	 * @param msg
	 * @param code
	 */
	public static void ifNull(Object obj, String msg, Integer code) {
		if (obj == null) {
			handleMsg(msg, code);
		}
	}
	public static void ifNull(Object obj, RuntimeException e) {
		if (obj == null) {
			throw e;
		}
	}
	public static void ifNull(Object obj, String msg) {
		ifNull(obj, msg, null);
	}
	public static void ifNull(Object obj, Integer code) {
		ifNull(obj, null, code);
	}
	public static void ifNull(Object obj) {
		ifNull(obj, null, null);
	}

	/**
	 * 如果不是null 则异常处理
	 * @param obj
	 * @param msg
	 * @param code
	 */
	public static void ifNotNull(Object obj, String msg, Integer code) {
		if (obj != null) {
			handleMsg(msg, code);
		}
	}
	public static void ifNotNull(Object obj, RuntimeException e) {
		if (obj != null) {
			throw e;
		}
	}
	public static void ifNotNull(Object obj, String msg) {
		ifNotNull(obj, msg, null);
	}
	public static void ifNotNull(Object obj, Integer code) {
		ifNotNull(obj, null, code);
	}
	public static void ifNotNull(Object obj) {
		ifNotNull(obj, null, null);
	}
	
	/**
	 * 如果不为empty且不是手机 则异常处理
	 * @param mobile
	 * @param msg
	 * @param code
	 */
	public static void ifNotMobile(String mobile, String msg, Integer code) {
		if(StringUtils.isNotEmpty(mobile)){
			if (!mobile.matches(MOBILE_REGX)) {
				handleMsg(msg, code);
			}
		}
	}
	public static void ifNotMobile(String mobile, RuntimeException e) {
		if(StringUtils.isNotEmpty(mobile)){
			if (!mobile.matches(MOBILE_REGX)) {
				throw e;
			}
		}
	}
	public static void ifNotMobile(String mobile, String msg) {
		ifNotMobile(mobile, msg, null);
	}
	public static void ifNotMobile(String mobile, Integer code) {
		ifNotMobile(mobile, null, code);
	}
	public static void ifNotMobile(String mobile) {
		ifNotMobile(mobile, null, null);
	}

	/**
	 * 如果为empty或者不是手机 则异常处理
	 * @param mobile
	 * @param msg
	 * @param code
	 */
	public static void ifRequireMobile(String mobile, String msg, Integer code) {
		if(StringUtils.isNotEmpty(mobile)){
			if (!mobile.matches(MOBILE_REGX)) {
				handleMsg(msg, code);
			}
		} else {
			handleMsg(msg, code);
		}
	}
	public static void ifRequireMobile(String mobile, RuntimeException e) {
		if(StringUtils.isNotEmpty(mobile)){
			if (!mobile.matches(MOBILE_REGX)) {
				throw e;
			}
		}else{
			throw e;
		}
	}
	public static void ifRequireMobile(String mobile, String msg) {
		ifRequireMobile(mobile, msg, null);
	}
	public static void ifRequireMobile(String mobile, Integer code) {
		ifRequireMobile(mobile, null, code);
	}
	public static void ifRequireMobile(String mobile) {
		ifRequireMobile(mobile, null, null);
	}

	/**
	 * 如果不为empty且不是邮箱 则异常处理
	 * @param email
	 * @param msg
	 * @param code
	 */
	public static void ifNotEmail(String email, String msg, Integer code) {
		if(StringUtils.isNotEmpty(email)){
			Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
			Matcher m = p.matcher(email);
			if(!m.matches()){
				handleMsg(msg, code);
			}
		}
	}
	public static void ifNotEmail(String email, RuntimeException e) {
		if(StringUtils.isNotEmpty(email)){
			Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
			Matcher m = p.matcher(email);
			if(!m.matches()){
				throw e;
			}
		}
	}
	public static void ifNotEmail(String email, String msg) {
		ifNotEmail(email, msg, null);
	}
	public static void ifNotEmail(String email, Integer code) {
		ifNotEmail(email, null, code);
	}
	public static void ifNotEmail(String email) {
		ifNotEmail(email, null, null);
	}

	/**
	 * 如果为empty或者不是邮箱 则异常处理
	 * @param email
	 * @param msg
	 * @param code
	 */
	public static void ifRequireEmail(String email, String msg, Integer code) {
		if(StringUtils.isNotEmpty(email)){
			Pattern p = Pattern.compile(EMAIL_REGX);
			Matcher m = p.matcher(email);
			if(!m.matches()){
				handleMsg(msg, code);
			}
		}else{
			handleMsg(msg, code);
		}
	}
	public static void ifRequireEmail(String email, RuntimeException e) {
		if(StringUtils.isNotEmpty(email)){
			Pattern p = Pattern.compile(EMAIL_REGX);
			Matcher m = p.matcher(email);
			if(!m.matches()){
				throw e;
			}
		} else {
			throw e;
		}
	}
	public static void ifRequireEmail(String email, String msg) {
		ifRequireEmail(email, msg, null);
	}
	public static void ifRequireEmail(String email, Integer code) {
		ifRequireEmail(email, null, code);
	}
	public static void ifRequireEmail(String email) {
		ifRequireEmail(email, null, null);
	}

	/**
	 * 如果数值不相等 则异常处理
	 * 
	 * @param a
	 * @param b
	 * @param msg
	 * @param code
	 */
	public static void ifNumNotEquals(Number a, Number b, String msg, Integer code) {
		if (!NumberUtil.numEquals(a, b)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNumNotEquals(Number a, Number b, RuntimeException e) {
		if (!NumberUtil.numEquals(a, b)) {
			throw e;
		}
	}
	public static void ifNumNotEquals(Number a, Number b, String msg) {
		ifNumNotEquals(a, b, msg, null);
	}
	public static void ifNumNotEquals(Number a, Number b, Integer code) {
		ifNumNotEquals(a, b, null, code);
	}
	public static void ifNumNotEquals(Number a, Number b) {
		ifNumNotEquals(a, b, null, null);
	}

	/**
	 * 如果数值相等 则异常处理
	 *
	 * @param a
	 * @param b
	 * @param msg
	 * @param code
	 */
	public static void ifNumEquals(Number a, Number b, String msg, Integer code) {
		if (NumberUtil.numEquals(a, b)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNumEquals(Number a, Number b, RuntimeException e) {
		if (NumberUtil.numEquals(a, b)) {
			throw e;
		}
	}
	public static void ifNumEquals(Number a, Number b, String msg) {
		ifNumEquals(a, b, msg, null);
	}
	public static void ifNumEquals(Number a, Number b, Integer code) {
		ifNumEquals(a, b, null, code);
	}
	public static void ifNumEquals(Number a, Number b) {
		ifNumEquals(a, b, null, null);
	}
	
	/**
	 * 如果数值不在数组中 则异常处理
	 * 
	 * @param a
	 * @param arr
	 * @param msg
	 * @param code
	 */
	public static void ifNumNotInArr(Number a, Number[] arr, String msg, Integer code) {
		if (!NumberUtil.numInArr(a, arr)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNumNotInArr(Number a, Number[] arr, RuntimeException e) {
		if (!NumberUtil.numInArr(a, arr)) {
			throw e;
		}
	}
	public static void ifNumNotInArr(Number a, Number[] arr, String msg) {
		ifNumNotInArr(a, arr, msg, null);
	}
	public static void ifNumNotInArr(Number a, Number[] arr, Integer code) {
		ifNumNotInArr(a, arr, null, code);
	}
	public static void ifNumNotInArr(Number a, Number[] arr) {
		ifNumNotInArr(a, arr, null, null);
	}
	
	/**
	 * 如果数值不在list中 则异常处理
	 * 
	 * @param a
	 * @param list
	 * @param msg
	 * @param code
	 */
	public static void ifNumNotInList(Number a, List<? extends Number> list, String msg, Integer code) {
		if (!NumberUtil.numInList(a, list)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNumNotInList(Number a, List<? extends Number> list, RuntimeException e) {
		if (!NumberUtil.numInList(a, list)) {
			throw e;
		}
	}
	public static void ifNumNotInList(Number a, List<? extends Number> list, String msg) {
		ifNumNotInList(a, list, msg, null);
	}
	public static void ifNumNotInList(Number a, List<? extends Number> list, Integer code) {
		ifNumNotInList(a, list, null, code);
	}
	public static void ifNumNotInList(Number a, List<? extends Number> list) {
		ifNumNotInList(a, list, null, null);
	}
	
	/**
	 * 如果字符串不在数组中 则异常处理
	 * 
	 * @param a
	 * @param arr
	 * @param msg
	 * @param code
	 */
	public static void ifStrNotInArr(String a, String[] arr, String msg, Integer code) {
		if (!StringUtil.strInArr(a, arr)) {
			handleMsg(msg, code);
		}
	}
	public static void ifStrNotInArr(String a, String[] arr, RuntimeException e) {
		if (!StringUtil.strInArr(a, arr)) {
			throw e;
		}
	}
	public static void ifStrNotInArr(String a, String[] arr, String msg) {
		ifStrNotInArr(a, arr, msg, null);
	}
	public static void ifStrNotInArr(String a, String[] arr, Integer code) {
		ifStrNotInArr(a, arr, null, code);
	}
	public static void ifStrNotInArr(String a, String[] arr) {
		ifStrNotInArr(a, arr, null, null);
	}
	
	/**
	 * 如果字符串不在list中 则异常处理
	 * 
	 * @param a
	 * @param list
	 * @param msg
	 * @param code
	 */
	public static void ifStrNotInList(String a, List<String> list, String msg, Integer code) {
		if (!StringUtil.strInList(a, list)) {
			handleMsg(msg, code);
		}
	}
	public static void ifStrNotInList(String a, List<String> list, RuntimeException e) {
		if (!StringUtil.strInList(a, list)) {
			throw e;
		}
	}
	public static void ifStrNotInList(String a, List<String> list, String msg) {
		ifStrNotInList(a, list, msg, null);
	}
	public static void ifStrNotInList(String a, List<String> list, Integer code) {
		ifStrNotInList(a, list, null, code);
	}
	public static void ifStrNotInList(String a, List<String> list) {
		ifStrNotInList(a, list, null, null);
	}
	
	/**
	 * 验证是否是对应格式的时间
	 * 
	 * @param date
	 * @param format
	 * @param msg
	 * @param code
	 * @return
	 */
	public static Date ifNotDate(String date, String format, String msg, Integer code) {
		if(StringUtils.isNotEmpty(date)){
			try {
				return new SimpleDateFormat(format).parse(date);
			} catch (ParseException e) {
				handleMsg(msg, code);
			}
		}
		return null;
	}
	public static Date ifNotDate(String date, String format, RuntimeException e) {
		if(StringUtils.isNotEmpty(date)){
			try {
				return new SimpleDateFormat(format).parse(date);
			} catch (ParseException pe) {
				throw e;
			}
		}
		return null;
	}
	public static Date ifNotDate(String date, String format, String msg) {
		return ifNotDate(date, format, msg, null);
	}
	public static Date ifNotDate(String date, String format, Integer code) {
		return ifNotDate(date, format, null, code);
	}
	public static Date ifNotDate(String date, String format) {
		return ifNotDate(date, format, null, null);
	}

	/**
	 * 验证是否是对应格式的时间
	 *
	 * @param date
	 * @param format
	 * @param msg
	 * @param code
	 * @return
	 */
	public static Date ifRequireDate(String date, String format, String msg, Integer code) {
		if(StringUtils.isNotEmpty(date)){
			try {
				return new SimpleDateFormat(format).parse(date);
			} catch (ParseException e) {
				handleMsg(msg, code);
			}
		} else {
			handleMsg(msg, code);
		}
		return null;
	}
	public static Date ifRequireDate(String date, String format, RuntimeException e) {
		if(StringUtils.isNotEmpty(date)){
			try {
				return new SimpleDateFormat(format).parse(date);
			} catch (ParseException pe) {
				throw e;
			}
		} else {
			throw e;
		}
	}
	public static Date ifRequireDate(String date, String format, String msg) {
		return ifRequireDate(date, format, msg, null);
	}
	public static Date ifRequireDate(String date, String format, Integer code) {
		return ifRequireDate(date, format, null, code);
	}
	public static Date ifRequireDate(String date, String format) {
		return ifRequireDate(date, format, null, null);
	}


	/**
	 * 如果字符串not equals 则异常处理
	 * 
	 * @param a
	 * @param b
	 * @param msg
	 * @param code
	 */
	public static void ifNotEquals(String a, String b, String msg, Integer code) {
		if (!StringUtils.equals(a, b)) {
			handleMsg(msg, code);
		}
	}
	public static void ifNotEquals(String a, String b, RuntimeException e) {
		if (!StringUtils.equals(a, b)) {
			throw e;
		}
	}
	public static void ifNotEquals(String a, String b, String msg) {
		ifNotEquals(a, b, msg, null);
	}
	public static void ifNotEquals(String a, String b, Integer code) {
		ifNotEquals(a, b, null, code);
	}
	public static void ifNotEquals(String a, String b) {
		ifNotEquals(a, b, null, null);
	}

	/**
	 * 如果字符串equals 则异常处理
	 *
	 * @param a
	 * @param b
	 * @param msg
	 * @param code
	 */
	public static void ifEquals(String a, String b, String msg, Integer code) {
		if (StringUtils.equals(a, b)) {
			handleMsg(msg, code);
		}
	}
	public static void ifEquals(String a, String b, RuntimeException e) {
		if (StringUtils.equals(a, b)) {
			throw e;
		}
	}
	public static void ifEquals(String a, String b, String msg) {
		ifEquals(a, b, msg, null);
	}
	public static void ifEquals(String a, String b, Integer code) {
		ifEquals(a, b, null, code);
	}
	public static void ifEquals(String a, String b) {
		ifEquals(a, b, null, null);
	}

	/**
	 * 如果obj为true 则异常处理
	 * @param obj
	 * @param msg
	 * @param code
	 */
	public static void ifTrue(boolean obj, String msg, Integer code) {
		if (obj) {
			handleMsg(msg, code);
		}
	}
	public static void ifTrue(boolean obj, RuntimeException e) {
		if (obj) {
			throw e;
		}
	}
	public static void ifTrue(boolean obj, String msg) {
		ifTrue(obj, msg, null);
	}
	public static void ifTrue(boolean obj, Integer code) {
		ifTrue(obj, null, code);
	}
	public static void ifTrue(boolean obj) {
		ifTrue(obj, null, null);
	}

	/**
	 * 如果obj为false 则异常处理
	 * @param obj
	 * @param msg
	 * @param code
	 */
	public static void ifFalse(boolean obj, String msg, Integer code) {
		if (!obj) {
			handleMsg(msg, code);
		}
	}
	public static void ifFalse(boolean obj, RuntimeException e) {
		if (!obj) {
			throw e;
		}
	}
	public static void ifFalse(boolean obj, String msg) {
		ifFalse(obj, msg, null);
	}
	public static void ifFalse(boolean obj, Integer code) {
		ifFalse(obj, null, code);
	}
	public static void ifFalse(boolean obj) {
		ifFalse(obj, null, null);
	}

	/**
	 * 如果数组null或者length 0 则异常处理
	 * @param obj
	 * @param msg
	 * @param code
	 */
	public static void ifArrEmpty(Object obj, String msg, Integer code) {
		if (obj == null || (obj.getClass().isArray() && Array.getLength(obj) < 1)) {
			handleMsg(msg, code);
		} else if(!obj.getClass().isArray()){
			handleMsg("参数不是数组", null);
		}
	}
	public static void ifArrEmpty(Object obj, RuntimeException e) {
		if (obj == null || (obj.getClass().isArray() && Array.getLength(obj) < 1)) {
			throw e;
		} else if(!obj.getClass().isArray()){
			handleMsg("参数不是数组", null);
			throw new RuntimeException("ValidateUtil参数不是数组");
		}
	}
	public static void ifArrEmpty(Object obj, String msg) {
		ifArrEmpty(obj, msg, null);
	}
	public static void ifArrEmpty(Object obj, Integer code) {
		ifArrEmpty(obj, null, code);
	}
	public static void ifArrEmpty(Object obj) {
		ifArrEmpty(obj, null, null);
	}

	/**
	 * 如果集合null或者size 0 则异常处理
	 * @param obj
	 * @param msg
	 * @param code
	 */
	public static void ifCollectionEmpty(Collection obj, String msg, Integer code) {
		if (obj == null || obj.size() < 1) {
			handleMsg(msg, code);
		}
	}
	public static void ifCollectionEmpty(Collection obj, RuntimeException e) {
		if (obj == null || obj.size() < 1) {
			throw e;
		}
	}
	public static void ifCollectionEmpty(Collection obj, String msg) {
		ifCollectionEmpty(obj, msg, null);
	}
	public static void ifCollectionEmpty(Collection obj, Integer code) {
		ifCollectionEmpty(obj, null, code);
	}
	public static void ifCollectionEmpty(Collection obj) {
		ifCollectionEmpty(obj, null, null);
	}

}
